package com.xiaoma.lib.store.fileStore

import android.annotation.SuppressLint
import android.content.Context
import android.os.Environment
import android.util.Log
import androidx.core.content.ContextCompat
import java.io.File

@SuppressLint("StaticFieldLeak")
object FileStoreManager {


    private const val ROOT_PATH = "xiaoma"

    private  val ROOT_FOLDER by lazy {
         File(ContextCompat.getExternalFilesDirs(mContext, null)[0].path
                + File.separator + ROOT_PATH)
            .apply {
                this.mkdirs()
            }
    }

    private lateinit var mContext: Context

    private val DATA_CACHE_FOLDER by lazy {
        File(ROOT_FOLDER,"cache")
            .apply {
                this.mkdirs()
            }
    }

    private val IMPORTANT_DATA_FOLDER by lazy {
        File(ROOT_FOLDER,"data")
            .apply {
                this.mkdirs()
            }
    }

    fun initContext(context: Context) {
        mContext = context.applicationContext
    }

    /**
     *
     * @author lolin
     * @time 2022/12/7 15:59
     * @return 返回磁盘缓存的文件夹
     */
    fun getDiskCacheFolder(): File? {
       return if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
            return File(Environment.getExternalStorageDirectory(), "com.xiaoma").apply {
                this.mkdirs()
            }
        }else null
    }

     /**
      *
      * @author lolin
      * @time 2022/12/7 16:55
      * @return 返回vpa存放的资源的文件夹
     */
    fun getDiskVpaFolder():File?{
       val packageContext =  kotlin.runCatching {
              mContext!!.createPackageContext(
                 "com.xiaoma.xmvpa",
                 Context.CONTEXT_INCLUDE_CODE
             )
         }.getOrNull()?:return null

         val sharedVpaFolder = ContextCompat.getExternalFilesDirs(
             packageContext,
             null
         )[0]?.run {
             File(this, "vpa_resource").apply {
                 this.mkdirs()
             }
         }
         Log.i("FileStoreManager", "getDiskVpaFolder: $sharedVpaFolder")
        return sharedVpaFolder
    }

    /**
     *
     * @time 2022/12/7 16:55
     * @return 返回vpa存放动作的资源的文件夹
     */
    fun getDiskVpaActionFolder(): File? {
        val packageContext =   kotlin.runCatching {
            mContext!!.createPackageContext(
                "com.xiaoma.xmvpa",
                Context.CONTEXT_INCLUDE_CODE
            )
        }.getOrNull()?:return null
        val sharedActionFolder = ContextCompat.getExternalFilesDirs(
            packageContext,
            null
        )[0]?.run {
            File(this, "vpa_action").apply {
                this.mkdirs()
            }
        }
        Log.i("FileStoreManager", "getDiskVpaActionFolder: $sharedActionFolder")
        return sharedActionFolder
    }

    /**
     * Server 下载引擎对应动作资源到XmVpa私有目录
     * @return 返回下载到的目标文件
     */
    fun getDownloadVpaActionFolder(): File? {
        val packageContext =   kotlin.runCatching {
            mContext!!.createPackageContext(
                "com.xiaoma.xmvpa",
                Context.CONTEXT_INCLUDE_CODE
            )
        }.getOrNull()?:return null
        val sharedActionFolder = ContextCompat.getExternalFilesDirs(
            packageContext,
            Environment.DIRECTORY_DOWNLOADS
        )[0]?.run {
            File(this, "vpa_action").apply {
                this.mkdirs()
            }
        }
        Log.i("FileStoreManager", "getDownloadVpaActionFolder: $sharedActionFolder")
        return sharedActionFolder
    }


    /**
     *
     * @author lolin
     * @time 2022/12/7 16:00
     * @return 返回data下缓存的文件夹
     */
    fun getDataCacheFolder() = DATA_CACHE_FOLDER

    /**
     *
     * @author lolin
     * @time 2022/12/7 16:04
     * @return 返回重要数据的文件夹，
     */
    fun getImportantDataFolder() = IMPORTANT_DATA_FOLDER

    /**
     *
     * @author lolin
     * @time 2022/12/7 16:06
     * @return 返回data下root文件夹
     */
    fun getDataRootFolder() = ROOT_FOLDER
}